<?php

class Route
{
    private $uri;
    private $parts = array();
    private $countParts;
    private $menu = 'def';
    private $recordId = 1;
    private $nameId = NULL;
    private $_db;
    private $page = NULL;


    public function __construct($uri, $params) 
    {
        $this->uri = $uri;
        $this->uriToArray();
        $this->_db = Db::getInstance($params);
    }
    
    private function uriToArray()
    {
        if ($this->uri == '/')
        {
            $this->countParts = 0;
            return;
        }
        $this->parts = explode('/', $this->uri);
        array_shift($this->parts);
        array_pop($this->parts);
        $this->countParts = count($this->parts);
        
        if (preg_match('/^page([0-9]+)$/', $this->parts[$this->countParts-1]))
        {
            $this->page = array_pop($this->parts);
            $this->countParts -= 1;
        }
    }
    
    private function selectMenu()
    {
        if ($this->countParts == 0)
        {
            return false;
        }
        
        $table = $this->parts[0].'_menu';
        if ($this->_db->table_exists($table))
        {
            $this->menu = $this->parts[0];
            array_shift($this->parts);
            $this->countParts -= 1;
            return true;
        }
        
        return false;
    }
    
    private function findLink ()
    {
        if ($this->countParts == 0)
        {
            $this->recordId = 1;
            return true;
        }
        
        $table = $this->menu.'_menu';
        
        if ($this->countParts == 1)
        {
            $r = $this->_db->q("select * from ".$table." where link = '/".$this->parts[0]."/'");
            
            if (mysql_num_rows($r))
            {
                $res = mysql_fetch_assoc($r);
                $this->recordId = $res['id'];
                return true;
            }
            else 
            {
                $r = $this->_db->q("select * from ".$table." where id = 1");
                $res = mysql_fetch_assoc($r);
                
                if (preg_match('/^short[A-Za-z0-9_.]*$/', $res['view']))
                {
                    $r = $this->_db->q("select * from ".$res['table']." where nameId = '".$this->parts[0]."'");
                    if (mysql_num_rows($r) == 0)
                    {
                        return false;
                    }
                    
                    $this->nameId = $this->parts[0];
                    return true;
                }
                
//проверка shorta главной
                return false;
            }
        }
        
        $tmp = $this->parts;
        $ur = implode('/', $tmp);
        $r = $this->_db->q("select * from ".$table." where link = '/".$ur."/'");
            
        if (mysql_num_rows($r))
        {
            $res = mysql_fetch_assoc($r);
            $this->recordId = $res['id'];
            return true;
        }
        
        $tmpNameId = array_pop($tmp);
        
        $ur = implode('/', $tmp);
        $r = $this->_db->q("select * from ".$table." where link = '/".$ur."/'");
            
        if (mysql_num_rows($r))
        {
            $res = mysql_fetch_assoc($r);
            
            if (preg_match('/^short[A-Za-z0-9_.]*$/', $res['view']))
            {
                $r = $this->_db->q("select * from ".$res['table']." where nameId = '".$tmpNameId."'");
                if (mysql_num_rows($r) == 0)
                {
                    return false;
                }
            }
            
            if (preg_match('/^long[A-Za-z0-9_.]*$/', $res['view']) && $res['record'] != NULL)
            {
                return true;
            }
            
            $this->nameId = $tmpNameId;
            $this->recordId = $res['id'];
            return true;
        }
        else 
        {
            return false;
        }
    }
    
    public function go ()
    {
        if ($this->countParts == 0)
        {
            $tpl = new Template($this->menu, $this->recordId);
            $tpl->viewTemplate();
            return;
        }
        
        if ($this->findLink() === false)
        {
            if ($this->selectMenu() === false)
            {
                echo "404";
            }
            else
            {
                if ($this->findLink() === false)
                {
                    echo '404';
                }
                else
                {
                    $tpl = new Template($this->menu, $this->recordId, $this->nameId, $this->page);
                    $tpl->viewTemplate();
                    return;
                }
            }
        }
        else
        {
            $tpl = new Template($this->menu, $this->recordId, $this->nameId, $this->page);
            $tpl->viewTemplate();
            return;
        }
    }
}